<template>
  <el-form label-width="100px" label-position="left">
    <el-form-item label="图层名称" prop="layerName">
      <el-input
        v-model.trim="formData.layerName"
        size="default"
        style="width: 100%"
        clearable
        @change="changed($event, 'layerName')"
      />
    </el-form-item>

    <el-collapse>
      <el-collapse-item title="表格设置" name="1">
        <el-form-item label="行高" prop="rowHeight">
          <el-input-number
            v-model.trim="formData.rowHeight"
            size="default"
            style="width: 100%"
            controls-position="right"
            @change="changed($event, 'rowHeight')"
          />
        </el-form-item>
      </el-collapse-item>
    </el-collapse>

    <el-collapse>
      <el-collapse-item title="表头设置" name="1">
        <el-form-item label="字体字号" prop="hFontSize">
          <el-input-number
            v-model.trim="formData.hFontSize"
            size="default"
            style="width: 100%"
            controls-position="right"
            @change="changed($event, 'hFontSize')"
          />
        </el-form-item>
        <el-form-item label="字体颜色" prop="hFontColor">
          <ColorPicker
            v-model.trim="formData.hFontColor"
            @change="(val) => changed(val, 'hFontColor')"
          />
        </el-form-item>
        <el-form-item label="字体间距" prop="hLetterSpacing">
          <el-input-number
            v-model.trim="formData.hLetterSpacing"
            size="default"
            style="width: 100%"
            controls-position="right"
            @change="changed($event, 'hLetterSpacing')"
          />
        </el-form-item>
        <el-form-item label="字体背景" prop="hBackgroundColor">
          <ColorPicker
            v-model.trim="formData.hBackgroundColor"
            @change="(val) => changed(val, 'hBackgroundColor')"
          />
        </el-form-item>
        <el-form-item label="文字粗细" prop="hFontWeight">
          <el-select
            v-model="formData.hFontWeight"
            clearable
            placeholder="请选择"
            @change="(val) => changed(val, 'hFontWeight')"
          >
            <el-option
              v-for="item in fontWeightOptions"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="对齐方式" prop="hTextAlign">
          <el-select
            v-model="formData.hTextAlign"
            clearable
            placeholder="请选择"
            @change="(val) => changed(val, 'hTextAlign')"
          >
            <el-option
              v-for="item in textAlignOptions"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
      </el-collapse-item>
    </el-collapse>

    <el-collapse>
      <el-collapse-item title="表体设置" name="1">
        <el-form-item label="字体字号" prop="bFontSize">
          <el-input-number
            v-model.trim="formData.bFontSize"
            size="default"
            style="width: 100%"
            controls-position="right"
            @change="changed($event, 'bFontSize')"
          />
        </el-form-item>
        <el-form-item label="字体颜色" prop="bFontColor">
          <ColorPicker
            v-model.trim="formData.bFontColor"
            @change="(val) => changed(val, 'bFontColor')"
          />
        </el-form-item>
        <el-form-item label="字体间距" prop="bLetterSpacing">
          <el-input-number
            v-model.trim="formData.bLetterSpacing"
            size="default"
            style="width: 100%"
            controls-position="right"
            @change="changed($event, 'bLetterSpacing')"
          />
        </el-form-item>

        <el-form-item label="文字粗细" prop="bFontWeight">
          <el-select
            v-model="formData.bFontWeight"
            clearable
            placeholder="请选择"
            @change="(val) => changed(val, 'bFontWeight')"
          >
            <el-option
              v-for="item in fontWeightOptions"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="对齐方式" prop="bTextAlign">
          <el-select
            v-model="formData.bTextAlign"
            clearable
            placeholder="请选择"
            @change="(val) => changed(val, 'bTextAlign')"
          >
            <el-option
              v-for="item in textAlignOptions"
              :key="item.code"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="奇行颜色" prop="bOddBackgroundColor">
          <ColorPicker
            v-model.trim="formData.bOddBackgroundColor"
            @change="(val) => changed(val, 'bOddBackgroundColor')"
          />
        </el-form-item>
        <el-form-item label="偶行颜色" prop="bEvenBackgroundColor">
          <ColorPicker
            v-model.trim="formData.bEvenBackgroundColor"
            @change="(val) => changed(val, 'bEvenBackgroundColor')"
          />
        </el-form-item>
      </el-collapse-item>
    </el-collapse>
  </el-form>
</template>
<script lang="ts" setup>
import { reactive, watch } from 'vue'
import ColorPicker from '../../../components/colorPicker.vue'

// 接收父组件传入的值
const props = defineProps<{
  modelValue: any
}>()

const emit = defineEmits(['update:modelValue', 'onChanged'])

const formData = reactive<any>({})

const fontWeightOptions = ref([
  { code: 'normal', name: '正常' },
  { code: 'bold', name: '粗体' },
  { code: 'bolder', name: '特粗体' },
  { code: 'lighter', name: '细体' }
])

const textAlignOptions = ref([
  { code: 'center', name: '居中' },
  { code: 'left', name: '左对齐' },
  { code: 'right', name: '右对齐' }
])

watch(
  () => props.modelValue,
  (newVal) => {
    Object.assign(formData, newVal)
  },
  { immediate: true, deep: true }
)

const changed = (val: any, key: string) => {
  emit('update:modelValue', { ...formData })
  emit('onChanged', { key, val })
}
</script>
<style lang="scss" scoped></style>
